package com.itheima.web.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import com.itheima.constant.MessageConstant;
import com.itheima.domain.Role;
import com.itheima.domain.Result;
import com.itheima.factory.BeanFactory;
import com.itheima.service.RoleService;
import com.itheima.utils.BeanUtil;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/system/role/*")
public class RoleServlet extends BaseServlet {
    RoleService roleService = BeanFactory.getBean("roleService",RoleService.class);
    private Result findAll(HttpServletRequest request, HttpServletResponse response) throws IOException {
        try {
            //2 调用service层方法，查询所有数据
            List<Role> list = roleService.findAll();
            //3 响应结果
            return new Result(true, MessageConstant.DELETE_ROLE_SUCCESS, list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_ROLE_FAIL);
        }
    }

    private Result delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //获取请求参数并封装
        try {
            String[] ids = BeanUtil.fillBeanFromJson(request, String[].class);
            //调用service层edit方法,执行删除操作
            roleService.delete(ids);
            //响应客户端
            return new Result(true,MessageConstant.DELETE_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,MessageConstant.DELETE_ROLE_FAIL);
        }
    }

    private Result edit(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //获取请求参数并封装
        try {
            Role role = BeanUtil.fillBeanFromJson(request, Role.class);
            //调用service层edit方法,执行保存操作
            roleService.edit(role);
            //响应客户端
            return new Result(true,MessageConstant.EDIT_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,MessageConstant.EDIT_ROLE_FAIL);
        }
    }

    private Result findById(HttpServletRequest request, HttpServletResponse response) throws IOException {
        try {
            //1.获取请求参数
            String roleId = request.getParameter("id");
            //调用service层方法,获取查询结果
            Role role = roleService.findById(roleId);
            //对客户端做出响应
            return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS,role);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_ROLE_FAIL);
        }
    }

    private Result save(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //获取请求参数并封装
        try {
            Role role = BeanUtil.fillBeanFromJson(request, Role.class);
            //调用service层save方法,执行保存操作
            roleService.save(role);
            //响应客户端
            return new Result(true,MessageConstant.SAVE_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,MessageConstant.SAVE_ROLE_FAIL);
        }
    }

    //处理分页查询的请求
    private Result findByPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            //1.获取请求参数
            String strCurrentPage = request.getParameter("currentPage");
            String strPageSize = request.getParameter("pageSize");
            //1.1判断非空
            int currentPage = 1,pageSize = 5;
            if(!StringUtils.isBlank(strCurrentPage)){
                currentPage = Integer.parseInt(strCurrentPage);
            }
            if(!StringUtils.isBlank(strPageSize)){
                pageSize = Integer.parseInt(strPageSize);
            }
            //调用service层方法,获取查询结果
            PageInfo<Role> pageInfo = roleService.findByPage(currentPage,pageSize);
            //对客户端做出响应
            return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS,pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_ROLE_FAIL);
        }
    }

    //更新角色绑定的模块
    private Result updateRoleAndModules(HttpServletRequest request, HttpServletResponse response) throws IOException {
        try {
            //1.获取请求参数
            //获取角色id
            String roleId = request.getParameter("roleId");
            //获取被选中的模块id数组
            String[] moduleIds = BeanUtil.fillBeanFromJson(request, String[].class);
            //调用service层方法,获取查询结果
            roleService.updateRoleAndModules(roleId,moduleIds);
            //对客户端做出响应
            return new Result(true, MessageConstant.SETTING_ROLE_AUTHORIZE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.SETTING_ROLE_AUTHORIZE_FAIL);
        }
    }

    //查询用户已经绑定的角色id们
    private Result findRoleIdsByUserId(HttpServletRequest request, HttpServletResponse response) throws IOException {
        try {
            //1.获取请求参数
            String userId = request.getParameter("userId");
            //调用service层方法,获取查询结果
            String[] checkedRoleIds = roleService.findRoleIdsByUserId(userId);
            //对客户端做出响应
            return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS,checkedRoleIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_ROLE_FAIL);
        }
    }
}
